package com.astudio.lhasa;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.astudio.lhasa.model.City;
import com.astudio.lhasa.model.TourSpot;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;

import java.util.ArrayList;
import java.util.List;

public class BaiduMapActivity extends AppCompatActivity implements OnGetSuggestionResultListener {

    private static final String TAG = BaiduMapActivity.class.getSimpleName();
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    int[] markers = {
            R.drawable.icon_marka, R.drawable.icon_markb, R.drawable.icon_markc,
            R.drawable.icon_markd, R.drawable.icon_marke, R.drawable.icon_markf,
            R.drawable.icon_markg, R.drawable.icon_markh, R.drawable.icon_marki,
    };

    private City city;
    private TourSpot spot;

    private SuggestionSearch suggestionSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baidu_map);
        setTitle("地图");

        mMapView = findViewById(R.id.mmap);
        mBaiduMap = mMapView.getMap();

        spot = getIntent().getParcelableExtra(TourSpotDetailActivity.TOUR_EXTRA);
        city = getIntent().getParcelableExtra(CityDetail.CITY_EXTRA);

        suggestionSearch = SuggestionSearch.newInstance();
        suggestionSearch.setOnGetSuggestionResultListener(this);
        suggestionSearch.requestSuggestion((new SuggestionSearchOption())
                .keyword(spot.getName())
                .city(city.getName()));
    }

    @Override
    public void setTitle(CharSequence title) {
        TextView tv = findViewById(R.id.title);
        tv.setText(title);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onGetSuggestionResult(SuggestionResult res) {
        if (res != null || res.getAllSuggestions() != null) {
            List<OverlayOptions> options = new ArrayList<>();
            int i = 0;
            for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
                if (i > 5) break;
                Log.d(TAG, "onGetSuggestionResult: " + info);
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(markers[i++]);
                OverlayOptions option = new MarkerOptions()
                        .position(info.getPt())
                        .icon(bitmap);
                options.add(option);
                mBaiduMap.addOverlays(options);
            }
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(res.getAllSuggestions().get(0).getPt()).zoom(18.0f);
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        }
        suggestionSearch.destroy();
    }
}
